//
//  active2ViewController.m
//  ui豆瓣
//
//  Created by lirui on 15/12/25.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "active2ViewController.h"
#import "actView.h"
#import "UIImageView+WebCache.h"
#import "dengluViewController.h"
#import "myTableViewCell.h"
#import "activemodel.h"
@interface active2ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)activemodel *m;
@end

@implementation active2ViewController
 -(void)viewWillAppear:(BOOL)animated
{
    
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"states"];
    if ([states isEqualToString:@"yes"]) {
        self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    }else
    {
        self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(denglu:)];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.navigationItem.title=self.md.title;
     actView *user = [[[NSBundle mainBundle] loadNibNamed:@"actView" owner:self options:nil] lastObject];
    user.center = CGPointMake(self.view.center.x, self.view.center.y);
    [self.view addSubview:user];
    user.activename.text = self.md.title;
    //user.activename.font = [ UIFont systemFontOfSize:20];
    user.time.text = self.md.begin_time;
    user.adresslabel. text = self.md.address;
    user.type.text = self.md.category_name;
    [user.headImageView  sd_setImageWithURL:[NSURL URLWithString:self.md.image]];
    user.holder.text = self.md.actOwner;
    user.actDetail.text =self.md.content;
    
    self.navigationItem.leftBarButtonItem = [[ UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(left:)];
    self.navigationController.navigationBar.tintColor = [ UIColor whiteColor];
//    self.navigationItem.rightBarButtonItem =[[ UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
   // [self collection];
    
    
}
-(void)collection
{
    self.tableview = [[ UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableview.backgroundColor =[ UIColor whiteColor];
    [self.view addSubview:_tableview];
    _tableview.dataSource =self;
    _tableview.delegate = self;
    [ _tableview registerNib:[UINib nibWithNibName:@"myTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    _tableview.rowHeight = 667;

    [self datahandeler];
    }


-(void)datahandeler
{
    NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    NSString *urlstr =[ str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    NSURL *url =[ NSURL URLWithString:urlstr];
    NSURLRequest *request =[ NSURLRequest requestWithURL:url];
    NSURLSession *session = [ NSURLSession sharedSession];
    NSURLSessionDataTask *task =[ session  dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            id result = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *arr = result[@"events"];
            self.arr = [NSMutableArray array];
            self.arr =[activemodel modelHanderWithArray:arr];
            NSLog(@"%@",_arr);
            [self.tableview reloadData];
        });
    }];
    
    [task resume];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    
//   self.m = _arr[indexPath.row];
//    NSLog(@"~~~%@",_m.title);
//    //cell.title.text = _m. title;
//   
//    //cell.actOwer.text = [ _arr[ indexPath.row] actOwer];
//    cell.style.text = [_arr[ indexPath.row] category_name];
//    cell.actDetail.text = [ _arr[indexPath.row] content];
    return cell;
    
}
-(void)left:(UINavigationItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)right:(UINavigationItem *)right
{

        UIAlertController *a = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功了" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *b = [UIAlertAction actionWithTitle:@"慎重" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"慎重啊");
            
        }];
        UIAlertAction *c = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"确定");
        }];
        
        [a addAction:b];
        [a addAction:c];
        [self presentViewController:a animated:YES completion:^{
    
        }];
    NSLog(@"%@",NSHomeDirectory());
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"a.plist"];
    NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@",self.md);
    if (arr) {
        for (activemodel *m in arr) {
            if ([m.title isEqualToString:_md.title]) {
                NSLog(@"已收藏了");
                return;
            }
        }
        [arr addObject:_md];
    
  }else
  {
      arr =[ NSMutableArray array];
      [arr addObject:_md];

  }
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

-(void)denglu:(UINavigationItem *)denglu1
{
    dengluViewController *denglu = [[ dengluViewController alloc] init];
    UINavigationController *navi = [[ UINavigationController alloc] initWithRootViewController:denglu];
    [self presentViewController:navi animated:YES completion:^{
        NSLog(@"进入登录界面");
    }];
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
